package com.atguigu.vodservice.serivce.impl;

import com.aiguigu.commonutils.ResultCode;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.vodservice.serivce.VideoService;
import com.atguigu.vodservice.util.AliyunVodSDKUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

import static com.atguigu.vodservice.util.VideoProperties.ACCESS_KEY_ID;
import static com.atguigu.vodservice.util.VideoProperties.ACCESS_KEY_SECRET;

/**
 * @title: VideoServiceImpl
 * @Author danghailiang
 * @Date: 2022/9/30 1:05
 * @Version 1.0
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Override
    public String uploadAlyVideo(MultipartFile file){
        // 文件原始名称
        String originalFilename = file.getOriginalFilename();
        // 上传阿里显示的名称
        String title = originalFilename.substring(0,originalFilename.lastIndexOf("."));
        String videoId = null;
        try {
            UploadStreamRequest request = new UploadStreamRequest(ACCESS_KEY_ID, ACCESS_KEY_SECRET, title + UUID.randomUUID(), originalFilename, file.getInputStream());
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            //请求视频点播服务的请求ID
            System.out.print("RequestId=" + response.getRequestId() + "\n");

            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.print("VideoId=" + response.getVideoId() + "\n");
            } else {
                videoId = response.getVideoId();
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return videoId;
    }

    @Override
    public void delAlyVideoById(String videoSourceId) {
        this.deleteVideo(videoSourceId);
    }

    @Override
    public void deleteBatch(List<String> ids) {
        String videoIds = StringUtils.join(ids.toArray(), ",");
        this.deleteVideo(videoIds);
    }

    private void deleteVideo(String ids){
        try{
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ACCESS_KEY_ID,
                    ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 1, 2, 3
            request.setVideoIds(ids);

            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (Exception e){
            throw new GuLiException(ResultCode.ERROR, "视频删除失败");
        }
    }

}
